home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / chasm01.sha / chasm.c next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  152 lines

  1. /*
  2.  *    chasm.c - main module
  3.  *
  4.  *    @(#)chasm.c    1.1 91/04/10
  5.  *
  6.  *    Copyright (c) 1991 Steve Scherf
  7.  *
  8.  *    Author:    Steve Scherf
  9.  *    Date:    Wed Apr 10 22:53:11 PDT 1991
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "chasm.h"
  16.  
  17. char *whatstr = "@(#) chasm v0.1    Copyright (c) 1991 Steve Scherf";
  18.  
  19. int aflag;            /* true if ascii output */
  20. int oflag;            /* true if redfined output filename */
  21.  
  22. char *infile;
  23. char *outfile;
  24. char *cout    = "c.out";
  25. char *cascii    = "c.ascii";
  26. char *ctmp    = "c.tmp";
  27. char *xstrrchr();
  28.  
  29. FILE *ifp;
  30. FILE *ofp;
  31.  
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char **argv;
  36. {
  37.     int i;
  38.     char *name;
  39.  
  40.     name = *argv;
  41.     infile = argv[argc-1];
  42.  
  43.     if(!--argc)
  44.         usage(name);
  45.  
  46.     for(argv++; *argv && argc > 1; argv += 1 + i, argc -= 1 + i) {
  47.         if(**argv != '-')
  48.             usage(name);
  49.         i = 0;
  50.         while(*(++*argv))
  51.             switch(**argv) {
  52.                 case 'a':
  53.                     aflag++;
  54.                     break;
  55.                 case 'o':
  56.                     if(!(outfile = *(argv+1)))
  57.                         usage(name);
  58.                     oflag++;
  59.                     i++;
  60.                     break;
  61.                 default:
  62.                     usage(name);
  63.             }
  64.     }
  65.  
  66.     if(!oflag)
  67.         if(aflag)
  68.             outfile = cascii;
  69.         else
  70.             outfile = cout;
  71.  
  72.     if((ifp = fopen(infile, "r")) == 0) {
  73.         fprintf(stderr, "%s: cannot open %s\n", name, infile);
  74.         exit(1);
  75.     }
  76.  
  77.     if((ofp = fopen(ctmp, "w")) == 0) {
  78.         fprintf(stderr, "%s: cannot open %s\n", name, ctmp);
  79.         exit(1);
  80.     }
  81.  
  82.     init_links();
  83.     yyparse();
  84.     chk_var();
  85.  
  86.     if(chk_size() || (chk_val() + chk_lab()) || pars_err ||
  87.         !out_inst(ofp, aflag)) {
  88.         if(unlink(ctmp))
  89.             perror(ctmp);
  90.         printf("No output written to %s\n", outfile);
  91.         exit(1);
  92.     }
  93.  
  94.     if(sunlink(outfile))
  95.         exit(1);
  96.  
  97.     if(link(ctmp, outfile)) {
  98.         fprintf(stderr, "Cannot link %s to %s\n", ctmp, outfile);
  99.         sunlink(ctmp);
  100.         exit(1);
  101.     }
  102.  
  103.     sunlink(ctmp);
  104. }
  105.  
  106.  
  107. /* unlink only if file exists */
  108. sunlink(n)
  109. char *n;
  110. {
  111.     if(!access(n, 0) && unlink(n)) {
  112.         perror(n);
  113.         return -1;
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
  119.  
  120. /* print usage message and exit */
  121. usage(name)
  122. char *name;
  123. {
  124.     char *n;
  125.  
  126.     if(n = xstrrchr(name, '/'))
  127.         n++;
  128.     else
  129.         n = name;
  130.  
  131.     fprintf(stderr, "Usage: %s [-a] [-o output_file] input_file\n", n);
  132.     exit(1);
  133. }
  134.  
  135.  
  136. /* return a pointer to the char c in s, null if nonexistent */
  137. char *
  138. xstrrchr(s, c)
  139. register char *s;
  140. register char c;
  141. {
  142.     register char *p = 0;
  143.  
  144.     while(*s) {
  145.         if(*s == c)
  146.             p = s;
  147.         s++;
  148.     }
  149.  
  150.     return p;
  151. }
  152.